package com.ganeshkrishnan.globald.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ganeshkrishnan.globald.dao.CivResourceDao;
import com.ganeshkrishnan.globald.dao.MilResourceDao;
import com.ganeshkrishnan.globald.dao.PersonDao;
import com.ganeshkrishnan.globald.model.Person;
import com.google.code.facebookapi.FacebookException;
import com.google.code.facebookapi.FacebookJsonRestClient;
import com.google.code.facebookapi.ProfileField;

@Controller
@RequestMapping(value = "/invite")
public class InviteController {
	Long _person = 13l;
	@Autowired
	private CivResourceDao civicDao;
	@Autowired
	private MilResourceDao milDao;

	public MilResourceDao getMilDao() {
		return milDao;
	}

	public void setMilDao(MilResourceDao milDao) {
		this.milDao = milDao;
	}

	public CivResourceDao getCivicDao() {
		return civicDao;
	}

	public void setCivicDao(CivResourceDao civicDao) {
		this.civicDao = civicDao;
	}

	@Autowired
	private PersonDao personDao;

	public PersonDao getPersonDao() {
		return personDao;
	}

	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

	public InviteController() {
	}

	// initializing method
	@RequestMapping(method = RequestMethod.GET)
	public String getCreateForm(Model model, HttpSession session) {
		Long personID = (Long) session.getAttribute("personID");
		String sessionKey = (String) session.getAttribute("restSearchAppSession");
		String apiKey = "0bef528b7c0fb390bc2d75112be60b92";
		String secretKey = "43f82864988c4f6196fa04be5b690133";
		if (null == personID) {
			personID = _person;
		}
		try {
			FacebookJsonRestClient client = new FacebookJsonRestClient(apiKey, secretKey, sessionKey);
			Set<ProfileField> c = new HashSet<ProfileField>();
			c.add(ProfileField.UID);
			c.add(ProfileField.FIRST_NAME);
			//c.add(ProfileField.LAST_NAME);
			//c.add(ProfileField.PIC_SQUARE);
			//c.add(ProfileField.PIC_SMALL);
			//c.add(ProfileField.PIC_BIG);
			c.add(ProfileField.PIC);
			//c.add(ProfileField.PIC_SMALL_WITH_LOGO);
			List<Long> friends = new ArrayList<Long>();
			Map<String, String> mapAllData = new HashMap<String, String>();
			JSONArray jfriends = (JSONArray) client.friends_get();
			for (int flen = 0; flen < jfriends.length(); flen++) {
				friends.add(Long.valueOf(jfriends.get(flen).toString()));
			}
			JSONArray friendsData = (JSONArray) client.users_getInfo(friends, c);
			for (int flen = 0; flen < friendsData.length(); flen++) {
				JSONObject obj = (JSONObject) friendsData.get(flen);
				Long uid = obj.getLong(ProfileField.UID.toString());
				String fname = obj.getString(ProfileField.FIRST_NAME.toString());//+ " " + obj.getString(ProfileField.LAST_NAME.toString()); 
				String fpic = obj.getString(ProfileField.PIC.toString());
				mapAllData.put(fpic, fname);
				model.addAttribute("fdata", mapAllData);
			}
		}
		catch (FacebookException e) {
			
			e.printStackTrace();
		}
		catch (JSONException e) {

			e.printStackTrace();
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		Person p = personDao.get(personID);
		model.addAttribute("person", p);
		return "invite";
	}




}
